﻿
Public Class percentage_t
    Implements ICloneable, IComparable, IComparable(Of percentage_t), IComparable(Of SByte)

    Public Const _NOT_START As SByte = -1
    Public Const _FAILED As SByte = -2
    Public Const _FINISHED As SByte = 100

    Private v As SByte = 0

    Public Shared Function NOT_START() As percentage_t
        Return New percentage_t(_NOT_START)
    End Function

    Public Shared Function FAILED() As percentage_t
        Return New percentage_t(_FAILED)
    End Function

    Public Shared Function FINISHED() As percentage_t
        Return New percentage_t(_FINISHED)
    End Function

    Public Shared Operator +(ByVal this As percentage_t) As SByte
        If this Is Nothing Then
            Return _NOT_START
        Else
            Return this.v
        End If
    End Operator

    Public Shared Widening Operator CType(ByVal this As SByte) As percentage_t
        Return New percentage_t(this)
    End Operator

    Public Shared Widening Operator CType(ByVal this As percentage_t) As SByte
        Return (+this)
    End Operator

    Public Shared Operator =(ByVal this As percentage_t, ByVal that As percentage_t) As Boolean
        Return operatorEqual(this, that)
    End Operator

    Public Shared Operator <>(ByVal this As percentage_t, ByVal that As percentage_t) As Boolean
        Return operatorUnequal(this, that)
    End Operator

    Public Shared Operator =(ByVal this As percentage_t, ByVal that As SByte) As Boolean
        Return operatorEqual(this, that)
    End Operator

    Public Shared Operator <>(ByVal this As percentage_t, ByVal that As SByte) As Boolean
        Return operatorUnequal(this, that)
    End Operator

    Public Shared Operator =(ByVal this As SByte, ByVal that As percentage_t) As Boolean
        Return that = this
    End Operator

    Public Shared Operator <>(ByVal this As SByte, ByVal that As percentage_t) As Boolean
        Return that <> this
    End Operator

    Public Shared Operator <(ByVal this As percentage_t, ByVal that As percentage_t) As Boolean
        Return operatorLess(this, that)
    End Operator

    Public Shared Operator >(ByVal this As percentage_t, ByVal that As percentage_t) As Boolean
        Return operatorMore(this, that)
    End Operator

    Public Shared Operator <(ByVal this As percentage_t, ByVal that As SByte) As Boolean
        Return operatorLess(this, that)
    End Operator

    Public Shared Operator >(ByVal this As percentage_t, ByVal that As SByte) As Boolean
        Return operatorMore(this, that)
    End Operator

    Public Shared Operator <(ByVal this As SByte, ByVal that As percentage_t) As Boolean
        Return that > this
    End Operator

    Public Shared Operator >(ByVal this As SByte, ByVal that As percentage_t) As Boolean
        Return that < this
    End Operator

    Private Sub _new(ByVal p As percentage_t)
        If p Is Nothing Then
            _new()
        Else
            _new(p.v)
        End If
    End Sub

    Private Sub _new(ByVal p As SByte)
        v = p
    End Sub

    Private Sub _new(ByVal p As Object)
        Try
            _new(Convert.ToSByte(p))
        Catch
            raiseError("cannot convert " + Convert.ToString(p) + " to SByte", errorHandle.errorType.exclamation)
            _new()
        End Try
    End Sub

    Private Sub _new()
        _new(_NOT_START)
    End Sub

    Public Sub New(ByVal p As percentage_t)
        _new(p)
    End Sub

    Public Sub New(ByVal p As SByte)
        _new(p)
    End Sub

    Public Sub New(ByVal p As Int32)
        _new(p)
    End Sub

    Public Sub New(ByVal p As Int64)
        _new(p)
    End Sub

    Public Sub New()
        _new()
    End Sub

    Public Function Clone() As Object Implements ICloneable.Clone
        If Not Me Is Nothing Then
            Return New percentage_t(v)
        Else
            Return New percentage_t(_NOT_START)
        End If
    End Function

    Public Function CompareTo(ByVal obj As Object) As Int32 Implements IComparable.CompareTo
        If istype(Of SByte, Object)(obj) Then
            Return CompareTo(convertor.cast(Of SByte)(obj))
        Else
            Return CompareTo(convertor.cast(Of percentage_t)(obj))
        End If
    End Function

    Private Shared Function compareImpl(ByVal this As percentage_t, ByVal that As percentage_t) As Int32
        Return compare(this.v, that.v)
    End Function

    Public Function CompareTo(ByVal other As percentage_t) As Int32 Implements IComparable(Of percentage_t).CompareTo
        Dim object_compare_result As Int32
        object_compare_result = object_compare(Me, other)
        If object_compare_result = object_compare_undetermined Then
            Return compareImpl(Me, other)
        Else
            Return object_compare_result
        End If
    End Function

    Public Function CompareTo(ByVal other As SByte) As Integer Implements IComparable(Of SByte).CompareTo
        If Me Is Nothing Then
            Return -1
        Else
            Return compare(v, other)
        End If
    End Function

    Public Overrides Function ToString() As String
        Return Convert.ToString(v)
    End Function
End Class
